//
//  TTRecyclingHomeViewController.m
//  TaoTuo
//
//  Created by LG on 2020/8/15.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTRecyclingHomeViewController.h"
#import "TTRecyclingHomeTopView.h"
#import <UIView+TYAlertView.h>
#import <JXCategoryTitleView.h>
#import <JXCategoryIndicatorLineView.h>
#import <JXPagingView/JXPagerView.h>
#import <JXCategoryListContainerView.h>
#import "TTJianLouView.h"
#import "TTTeJiaView.h"
#import "TTRecyclingHomeRightNavbarViewManager.h"
#import "TTRecyclingUploadGoodsViewController.h"
#import "TTRecyclingNavigationViewController.h"

@interface TTRecyclingHomeViewController ()<JXPagerViewDelegate, JXCategoryViewDelegate>

@property (nonatomic, strong) JXCategoryTitleView *categoryView;
@property (nonatomic, strong) JXPagerView *pagingView;
@property (nonatomic, strong) NSArray *titles;

@property (nonatomic, strong) TTRecyclingHomeTopView *topView;
@property (nonatomic, strong) NSArray<UIView<JXPagerViewListViewDelegate> *> *listViews;
@property (nonatomic, strong) TTJianLouView *jianLouView;
@property (nonatomic, strong) TTTeJiaView *teJiaView;

@property (nonatomic, assign) NSInteger currentIndex;

@end

@implementation TTRecyclingHomeViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont boldSystemFontOfSize:18]};
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"hiushounav_bg"] forBarMetrics:UIBarMetricsDefault];
    TTRecyclingNavigationViewController *nav = (TTRecyclingNavigationViewController *)self.navigationController;
    [nav.segment setItems:@[[UIImage imageNamed:@"dianputhreedot_white"], [UIImage imageNamed:@"dianpuquan_white"]]];
    [self requestTopDatas];
    
    [[TTRecyclingHomeRightNavbarViewManager sharedManager] showRightBarView];
    
//    [TTRecyclingHomeRightNavbarViewManager sharedManager].rightBarViewBlock = ^(NSInteger index) {
//        TTRecyclingUploadGoodsViewController *uploadVC = [[TTRecyclingUploadGoodsViewController alloc] init];
//        [self.navigationController pushViewController:uploadVC animated:YES];
//    };
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor blackColor], NSFontAttributeName : [UIFont boldSystemFontOfSize:18]};
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    TTRecyclingNavigationViewController *nav = (TTRecyclingNavigationViewController *)self.navigationController;
    [nav.segment setItems:@[[UIImage imageNamed:@"dianputhreedot"], [UIImage imageNamed:@"dianpuquan"]]];
    
//    [TTRecyclingHomeRightNavbarViewManager sharedManager].rightBarViewBlock = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"高价回收";
    self.navigationItem.leftBarButtonItem = nil;
    
    self.topView = [TTRecyclingHomeTopView createViewFromNib];
    [self.view addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_topLayoutGuideBottom);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(220);
    }];
    
    
    [self.view layoutIfNeeded];
    [LLLineUtilies addLineInView:self.topView startPoint:CGPointMake(15, self.topView.bottom - 1) endPoint:CGPointMake(kScreenWidth - 15, self.topView.bottom - 1) lineColor:UIColorHex(F0F0F0)];
    // 初始化JXCategoryTitleView
    self.titles = @[@"低价捡漏", @"特价专区"];
    self.categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0, self.topView.bottom, self.view.width, 44)];
    self.categoryView.delegate = self;
    self.categoryView.titleColor = UIColorHex(666666);
    self.categoryView.titleSelectedColor = UIColorHex(333333);
    self.categoryView.titles = self.titles;
    self.categoryView.titleColorGradientEnabled = YES;
    self.categoryView.contentEdgeInsetLeft = 60*kScale;        // 左边距
    self.categoryView.contentEdgeInsetRight = 60*kScale;       // 右边距
    [self.view addSubview:self.categoryView];
    
    // 添加指示器
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    lineView.indicatorColor = UIColorHex(9B71FF);
    lineView.indicatorHeight = 6;
    lineView.verticalMargin = 8;
    lineView.indicatorCornerRadius = 3;
    self.categoryView.indicators = @[lineView];
    
    
    _pagingView = [[JXPagerView alloc] initWithDelegate:self];
    _pagingView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - StatusBarAndNavBar_Height);
    [self.view addSubview:self.pagingView];
    
    self.categoryView.contentScrollView = self.pagingView.listContainerView.collectionView;
    

    UIView *lineView1 = [[UIView alloc] init];
    lineView1.backgroundColor = UIColorHex(EEEEEE);
    [self.categoryView addSubview:lineView1];
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(8);
        make.bottom.equalTo(self.categoryView.mas_bottom).mas_offset(-8);
        make.width.mas_equalTo(1);
        make.centerX.equalTo(self.categoryView);
    }];
    
    CGRect rect = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - StatusBarAndNavBar_Height - self.categoryView.height);
    
    self.jianLouView = [[TTJianLouView alloc] initWithFrame:rect];
    self.teJiaView = [[TTTeJiaView alloc] initWithFrame:rect];
    self.listViews = @[self.jianLouView, self.teJiaView];
    
    
    // 刷新
    @weakify(self)
    self.pagingView.mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self)
        
        // 上部分数据
        [self requestTopDatas];
        // 只刷新当前所在的列表数据
        UIView *currentView = self.listViews[self.currentIndex];
        if (self.currentIndex == 0) {
            TTJianLouView *view = (TTJianLouView *)currentView;
            [view refreshDatas];
        } else {
            TTTeJiaView *view = (TTTeJiaView *)currentView;
            [view refreshDatas];
        }
        
    }];
    
    
}


#pragma mark - JXPagingViewDelegate
- (NSInteger)numberOfListsInPagerView:(JXPagerView *)pagerView
{
    return self.listViews.count;
}

- (UIView *)tableHeaderViewInPagerView:(JXPagerView *)pagerView {
    return self.topView;
}

- (NSUInteger)tableHeaderViewHeightInPagerView:(JXPagerView *)pagerView {
    return 220;
}

- (NSUInteger)heightForPinSectionHeaderInPagerView:(JXPagerView *)pagerView {
    return 44;
}

- (UIView *)viewForPinSectionHeaderInPagerView:(JXPagerView *)pagerView {
    return self.categoryView;
}

- (NSArray<UIView<JXPagerViewListViewDelegate> *> *)listViewsInPagerView:(JXPagerView *)pagerView {
    return self.listViews;
}

- (void)mainTableViewDidScroll:(UIScrollView *)scrollView
{
//    NSLog(@">>> scr == %f", scrollView.contentOffset.y);
}

- (id<JXPagerViewListViewDelegate>)pagerView:(JXPagerView *)pagerView initListAtIndex:(NSInteger)index
{
    id<JXPagerViewListViewDelegate> view = self.listViews[index];
    return view;
}

#pragma mark - JXCategoryViewDelegate
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    
    self.currentIndex = index;
    
}

#pragma mark - 网络请求
/// 回收页面上部分数据
- (void)requestTopDatas
{
    [[LLNetworkManagerTool sharedManager] requestMethod:GET urlString:recycling_home_top_api parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.topView.dic = responseObject;
        
        [self.pagingView.mainTableView.mj_header endRefreshing];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
        [self.pagingView.mainTableView.mj_header endRefreshing];
        
    } showLoadingInView:nil];
}



@end
